import matplotlib.pyplot as plt  
import matplotlib.path as mpath  
import matplotlib.patches as patches  
import matplotlib.transforms as transforms
import seaborn as sns
import random

color = sns.husl_palette(5,h=15/360, l=.65, s=1).as_hex()  
random.shuffle(color)
colors = dict(zip(["id" + str(i) for i in range(5)],color))

# 定义三角形的三个顶点  
verts = [  
    (0.0, 0.5),  # 顶部顶点  
    (-0.06, 0.45),
    (-0.07, 0.35),
    (-0.6, 0.3),
    (-0.6, 0.2),
    (-0.07, 0.2),
    (-0.07, -0.27),
    (-0.45, -0.3),
    (-0.45, -0.4),
    (-0.06, -0.4),
    (-0.04, -0.5),
    # 另一半
    (0.04, -0.5),
    (0.06, -0.4),
    (0.45, -0.4),
    (0.45, -0.3),
    (0.07, -0.27),
    (0.07, 0.2),
    (0.6, 0.2),
    (0.6, 0.3),
    (0.07, 0.35),
    (0.06, 0.45),
    (0.0, 0.5)
]  
  
# 创建与顶点对应的codes数组  
codes = [  
    mpath.Path.MOVETO,  # 移动到第一个顶点  
    mpath.Path.LINETO, 
    mpath.Path.LINETO, 
    mpath.Path.LINETO,  
    mpath.Path.LINETO,  
    mpath.Path.LINETO,
    mpath.Path.LINETO,
    mpath.Path.LINETO, 
    mpath.Path.LINETO, 
    mpath.Path.LINETO,  
    mpath.Path.LINETO,  
    mpath.Path.LINETO,
    mpath.Path.LINETO,
    mpath.Path.LINETO,
    mpath.Path.LINETO,
    mpath.Path.LINETO, 
    mpath.Path.LINETO, 
    mpath.Path.LINETO,  
    mpath.Path.LINETO,  
    mpath.Path.LINETO,
    mpath.Path.LINETO,
    mpath.Path.CLOSEPOLY  # 闭合路径，形成实心三角形  
]  
  
# 创建Path对象  
icon = mpath.Path(verts, codes)  
  
# 创建PathPatch对象，用于绘制实心三角形  
patch = patches.PathPatch(icon, facecolor=colors["id1"], lw=0.5)  # , edgecolor='black'
if __name__ == "__main__":
    # 创建一个新的图形和坐标轴  
    fig, ax = plt.subplots()  
    # ax.grid(False)

    # 创建一个旋转变换，旋转45度  
    transform = transforms.Affine2D().rotate_deg(0) + ax.transData  
    
    # 将旋转变换应用到PathPatch对象上  
    patch.set_transform(transform) 

    # 将PathPatch对象添加到坐标轴上  
    ax.add_patch(patch)  
    
    # 设置坐标轴限制以适应三角形  
    ax.set_xlim(-0.7, 0.7)  
    ax.set_ylim(-0.7, 0.7)  

    
    # 显示图形  
    plt.show()